/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 17.06.13
 * Time: 2:45
 * To change this template use File | Settings | File Templates.
 */
package {
import flash.geom.Point;

import mx.collections.ArrayCollection;
import mx.core.UIComponent;

public class GraphView extends UIComponent {
    private var canvas:UIComponent;
    private var axisCenter:Point = new Point();
    private var points:ArrayCollection = new ArrayCollection();
    private var _graphScaleX:Number = 10;
    private var _graphScaleY:Number = 1;
    private var movement:Number = 0;

    public function GraphView() {
        drawGraph();
    }

    public function setRect(x:int, y:int, width:int, height:int):void {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

        axisCenter = new Point(0, height / 2);

        try {
            removeChild(canvas);
        } catch (err:Error) {
            trace(err);
        }
        canvas = new UIComponent();
        canvas.width = width;
        canvas.height = height;
        this.addChild(canvas);

        canvas.graphics.lineStyle(1, 0xcccccc);

        // Draw axis
        canvas.graphics.moveTo(0, axisCenter.y);
        canvas.graphics.lineTo(this.width, axisCenter.y);
        for (var i:int = 0; i < this.width; i+=graphScaleX) {
            canvas.graphics.moveTo(i, axisCenter.y - 10);
            canvas.graphics.lineTo(i, axisCenter.y + 10);
        }

        // Draw border
        canvas.graphics.moveTo(0, 0);
        canvas.graphics.lineTo(this.width, 0);
        canvas.graphics.lineTo(this.width, this.height);
        canvas.graphics.lineTo(0, this.height);
        canvas.graphics.lineTo(0, 0);

        drawGraph();
    }

    private function drawGraph():void {
        // TODO: Draw axis here with replacement value

        graphics.clear();
        graphics.lineStyle(2, 0x0);
        if (points != null && points.length > 0)
            for (var i:int = 0; i < points.length - 2; i++)
                if (points.getItemAt(i).x - movement > 0) {
                    graphics.moveTo(
                            axisCenter.x + points.getItemAt(i).x - movement,
                            axisCenter.y + points.getItemAt(i).y);
                    for (var j:int = i + 1; j < points.length - 2; j++) {
                        graphics.lineTo(
                                axisCenter.x + points.getItemAt(j).x - movement,
                                axisCenter.y + points.getItemAt(j).y)
                    }
                    break;
                }
    }

    public function addPoint(point:Point):void {
        point.x *= graphScaleX;
        point.y *= graphScaleY;

        if (points == null)
            points = new ArrayCollection();

        points.addItem(point);
        if (point.x > this.width)
            movement = point.x - this.width;
        drawGraph();
    }

    public function removeAllPoint():void {
        points = new ArrayCollection();
        drawGraph();
    }

    public function get graphScaleX():Number {
        return _graphScaleX;
    }

    public function set graphScaleX(value:Number):void {
        _graphScaleX = value;
    }

    public function get graphScaleY():Number {
        return _graphScaleY;
    }

    public function set graphScaleY(value:Number):void {
        _graphScaleY = value;
    }
}
}
